﻿using UnityEngine;
using UnityEditor;
using UnityEditor.UI;
using UnityEngine.UI;

namespace Miao.UI
{
    [CustomEditor(typeof(ConstrainedContentSizeFitter))]
    [CanEditMultipleObjects]
    public class ConstrainedContentSizeFitterEditor : ContentSizeFitterEditor
    {
        SerializedProperty m_MaxWidth;
        SerializedProperty m_MaxHeight;
        SerializedProperty m_EnableWidthConstraint;
        SerializedProperty m_EnableHeightConstraint;

        protected override void OnEnable()
        {
            base.OnEnable();
            m_MaxWidth = serializedObject.FindProperty("maxWidth");
            m_MaxHeight = serializedObject.FindProperty("maxHeight");
            m_EnableWidthConstraint = serializedObject.FindProperty("enableWidthConstraint");
            m_EnableHeightConstraint = serializedObject.FindProperty("enableHeightConstraint");
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            EditorGUILayout.PropertyField(m_MaxWidth, true);
            EditorGUILayout.PropertyField(m_MaxHeight, true);
            EditorGUILayout.PropertyField(m_EnableWidthConstraint, true);
            EditorGUILayout.PropertyField(m_EnableHeightConstraint, true);
            serializedObject.ApplyModifiedProperties();

            //serializedObject.Update();

            //    // 绘制约束设置
            //    EditorGUILayout.Space();
            //    EditorGUILayout.LabelField("尺寸约束设置", EditorStyles.boldLabel);

            //    // 宽度约束
            //    EditorGUILayout.PropertyField(m_EnableWidthConstraint, new GUIContent("启用宽度约束"));
            //    if (m_EnableWidthConstraint.boolValue)
            //    {
            //        EditorGUI.indentLevel++;
            //        EditorGUILayout.PropertyField(m_MaxWidth, new GUIContent("最大宽度", "设置宽度的最大值，-1表示无限制"));
            //        EditorGUI.indentLevel--;
            //    }

            //    // 高度约束
            //    EditorGUILayout.PropertyField(m_EnableHeightConstraint, new GUIContent("启用高度约束"));
            //    if (m_EnableHeightConstraint.boolValue)
            //    {
            //        EditorGUI.indentLevel++;
            //        EditorGUILayout.PropertyField(m_MaxHeight, new GUIContent("最大高度", "设置高度的最大值，-1表示无限制"));
            //        EditorGUI.indentLevel--;
            //    }

            //    // 显示当前状态
            //    if (Application.isPlaying)
            //    {
            //        EditorGUILayout.Space();
            //        EditorGUILayout.LabelField("运行时信息", EditorStyles.boldLabel);

            //        ConstrainedContentSizeFitter fitter = (ConstrainedContentSizeFitter)target;
            //        RectTransform rectTransform = fitter.GetComponent<RectTransform>();

            //        if (rectTransform != null)
            //        {
            //            EditorGUILayout.LabelField("当前尺寸", $"{rectTransform.sizeDelta.x:F1} x {rectTransform.sizeDelta.y:F1}");
            //            EditorGUILayout.LabelField("首选尺寸", $"{LayoutUtility.GetPreferredSize(rectTransform, 0):F1} x {LayoutUtility.GetPreferredSize(rectTransform, 1):F1}");
            //        }

            //        EditorGUILayout.LabelField("最大宽度", fitter.GetMaxWidth().ToString("F1"));
            //        EditorGUILayout.LabelField("最大高度", fitter.GetMaxHeight().ToString("F1"));
            //    }

            //    EditorGUILayout.Space();

            //    // 绘制原始ContentSizeFitter的属性
            //    EditorGUILayout.LabelField("ContentSizeFitter设置", EditorStyles.boldLabel);
            //    base.OnInspectorGUI();

            //    // 添加实用按钮
            //    EditorGUILayout.Space();
            //    EditorGUILayout.BeginHorizontal();

            //    if (GUILayout.Button("刷新布局"))
            //    {
            //        ConstrainedContentSizeFitter fitter = (ConstrainedContentSizeFitter)target;
            //        fitter.OnTextChanged();
            //    }

            //    if (GUILayout.Button("重置约束"))
            //    {
            //        m_MaxWidth.floatValue = 300f;
            //        m_MaxHeight.floatValue = -1f;
            //        m_EnableWidthConstraint.boolValue = true;
            //        m_EnableHeightConstraint.boolValue = false;
            //    }

            //    //EditorGUILayout.EndHorizontal();

            //    //serializedObject.ApplyModifiedProperties();
        }
    }
}